<?php

namespace common\models\datas;

use common\components\extenders\MenuManage;
use common\components\helpers\Fun;
use frontend\models\datas\Admin;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "dmd_isuser_info".
 *
 * @property integer $id
 * @property string $name
 * @property string $contact
 * @property integer $mobile
 * @property integer $user_id
 * @property string $response_man
 * @property integer $customer_id
 * @property integer $created_at
 * @property integer $updated_at
 * @property string $username
 * @property string $password
 * @property string $parent_id
 * @property string $is_show
 */
class IsuserInfo extends \common\components\bases\BaseModel
{
    public $newpass;
    public $newpass_r;
    public $oldpass;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%isuser_info}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'customer_id', 'created_at', 'updated_at', 'response_man', 'status', 'site_id', 'is_show'], 'integer'],
            [['name', 'contact'], 'string', 'max' => 60],
            [['operation_type'], 'string', 'max' => 10],
            [['password'], 'string', 'min' => 6, 'max' => 60],
            [['mobile', 'response_man', 'is_show', 'contact'], 'required'],
            [['password'], 'required', 'message' => '请输入密码', 'on' => 'create'],
            [['mobile'], 'unique_mobile','on'=>'create'],
            [['mobile'], 'unique_mobile_wechat','on'=>'create_wechat'],
            [['password'], 'required', 'message' => '请输入密码', 'on' => 'create_wechat'],
            ['mobile', 'match', 'pattern' => '/^1([0-9]{9})/', 'message' => '手机号码格式错误'],
            ['mobile', 'string', 'max' => 11],
            [['oldpass', 'newpass', 'newpass_r'], 'required', 'on' => 'updatepass'],
            [['oldpass'], 'check_oldpass', 'on' => 'updatepass'],
            [['newpass'], 'check_newpass', 'on' => 'updatepass'],
            [['newpass_r'], 'check_newpass_r', 'on' => 'updatepass'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', 'ID'),
            'name' => Yii::t('common', '机构名称'),
            'contact' => Yii::t('common', '联系人'),
            'mobile' => Yii::t('common', '联系电话'),
            'user_id' => Yii::t('common', '添加人'),
            'response_man' => Yii::t('common', '负责人'),
            'customer_id' => Yii::t('common', '关联customer_id'),
            'created_at' => Yii::t('common', 'Created At'),
            'updated_at' => Yii::t('common', 'Updated At'),
            'status' => Yii::t('common', '状态'),
            'site_id' => Yii::t('common', '分店id'),
            'is_show' => Yii::t('common', '微信端是否显示'),
            'operation_type' => Yii::t('common', '可发类型'),
            'username' => Yii::t('common', '用户名'),
            'password' => Yii::t('common', '密码'),
            'oldpass' => Yii::t('zhtfrontend', '旧密码'),
            'newpass' => Yii::t('zhtfrontend', '新密码'),
            'newpass_r' => Yii::t('zhtfrontend', '确认密码'),
            'parent_id' => Yii::t('zhtfrontend', '父级id'),
        ];
    }

    public function extraFields()
    {
        return [
            'admin_name' => function () {
                return ($this->admin != null) ? $this->admin->username : '';
            },
            'response' => function () {
                return ($this->responseman != null) ? $this->responseman->username : '';
            },
            'backend_operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            },
        ];
    }

    public function fields()
    {
        $fields = parent::fields();

        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['updated_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->updated_at);
        };
        $fields['is_show'] = function () {
            switch ($this->is_show) {
                case 1:
                    return '是';
                case 0:
                    return '否';
            }
        };
        return $fields;
    }

    public function check_oldpass($attribute)
    {
        $isuserinfo = IsuserInfo::findOne(['id' => $this->id]);
        if ($isuserinfo->password != md5($this->$attribute)) {
            $this->addError($attribute, Yii::t('wechatzht', '旧密码不正确！'));
        }
    }

    public function check_newpass($attribute)
    {
        if ($this->oldpass == $this->$attribute) {
            $this->addError($attribute, Yii::t('wechatzht', '新密码不能与旧密码一样！'));
        }
    }

    public function check_newpass_r($attribute)
    {
        if ($this->newpass != $this->$attribute) {
            $this->addError($attribute, Yii::t('wechatzht', '两次密码不一致，请重新输入！'));
        }
    }

    public function unique_mobile($attribute){
        $row = IsuserInfo::find()->where(['mobile'=>$this->$attribute,'site_id'=>Fun::getSiteId()])->one();
        if($row != null){
            $this->addError($attribute, Yii::t('wechatzht', '此手机号已存在！'));
        }
    }

    public function unique_mobile_wechat($attribute){
        $row = IsuserInfo::find()->where(['mobile'=>$this->$attribute,'site_id'=>2])->one();
        if($row != null){
            $this->addError($attribute, Yii::t('wechatzht', '此手机号已存在！'));
        }
    }

    public function getAdmin()
    {
        return $this->hasOne(Admin::className(), ['id' => 'user_id']);
    }

    public function getResponseman()
    {
        return $this->hasOne(Admin::className(), ['id' => 'response_man']);
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],

        ]);
    }
}
